package com.wave.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.wave.common.utils.StringUtils;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.wave.system.domain.bo.SysDeviceSettingBo;
import com.wave.system.domain.vo.SysDeviceSettingVo;
import com.wave.system.domain.SysDeviceSetting;
import com.wave.system.mapper.SysDeviceSettingMapper;
import com.wave.system.service.ISysDeviceSettingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备设置Service业务层处理
 *
 * @author wave
 * @date 2024-10-11
 */
@RequiredArgsConstructor
@Service
public class SysDeviceSettingServiceImpl implements ISysDeviceSettingService {

    private final SysDeviceSettingMapper baseMapper;

    /**
     * 查询设备设置
     */
    @Override
    public SysDeviceSettingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设备设置列表
     */
    @Override
    public TableDataInfo<SysDeviceSettingVo> queryPageList(SysDeviceSettingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysDeviceSetting> lqw = buildQueryWrapper(bo);
        Page<SysDeviceSettingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备设置列表
     */
    @Override
    public List<SysDeviceSettingVo> queryList(SysDeviceSettingBo bo) {
        LambdaQueryWrapper<SysDeviceSetting> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysDeviceSetting> buildQueryWrapper(SysDeviceSettingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysDeviceSetting> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMcsPlcIp()), SysDeviceSetting::getMcsPlcIp, bo.getMcsPlcIp());
        lqw.eq(StringUtils.isNotBlank(bo.getToolBoardIp()), SysDeviceSetting::getToolBoardIp, bo.getToolBoardIp());
        lqw.eq(StringUtils.isNotBlank(bo.getMechanicalArm1Ip()), SysDeviceSetting::getMechanicalArm1Ip, bo.getMechanicalArm1Ip());
        lqw.eq(StringUtils.isNotBlank(bo.getMechanicalArm2Ip()), SysDeviceSetting::getMechanicalArm2Ip, bo.getMechanicalArm2Ip());
        lqw.eq(StringUtils.isNotBlank(bo.getPackagingLine1Ip()), SysDeviceSetting::getPackagingLine1Ip, bo.getPackagingLine1Ip());
        lqw.eq(StringUtils.isNotBlank(bo.getPackagingLine2Ip()), SysDeviceSetting::getPackagingLine2Ip, bo.getPackagingLine2Ip());
        lqw.eq(StringUtils.isNotBlank(bo.getPackagingLine3Ip()), SysDeviceSetting::getPackagingLine3Ip, bo.getPackagingLine3Ip());
        lqw.eq(StringUtils.isNotBlank(bo.getPackagingLine4Ip()), SysDeviceSetting::getPackagingLine4Ip, bo.getPackagingLine4Ip());
        lqw.eq(StringUtils.isNotBlank(bo.getDismantle1Ip()), SysDeviceSetting::getDismantle1Ip, bo.getDismantle1Ip());
        lqw.eq(StringUtils.isNotBlank(bo.getDismantle2Ip()), SysDeviceSetting::getDismantle2Ip, bo.getDismantle2Ip());
        return lqw;
    }

    /**
     * 新增设备设置
     */
    @Override
    public Boolean insertByBo(SysDeviceSettingBo bo) {
        SysDeviceSetting add = BeanUtil.toBean(bo, SysDeviceSetting.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备设置
     */
    @Override
    public Boolean updateByBo(SysDeviceSettingBo bo) {
        SysDeviceSetting update = BeanUtil.toBean(bo, SysDeviceSetting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysDeviceSetting entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
